YUI().use('json-parse','json-stringify', function (Y) {

  /**
   * some test ...
   **/
  graph = parseJSON();

  var node = {
    "id" : 5,
    "value" : 10,
    "children" : 2,
    "x" : 1000,
    "y" : 100
  };

  addNode(node);
  removeNode(1);
  edit(5,100,1,1);

  
  function parseJSON(){

    //opening file
    var req = new XMLHttpRequest();
    req.open('GET', 'sample.json', false);
    req.send(null);

    var jsonString = req.responseText;

    // JSON.parse throws a SyntaxError when passed invalid JSON
    try {
      var data = Y.JSON.parse(jsonString);
    }
    catch (e) {
      alert("Invalid data!");
    }

    return data.graph;

  }

  /**
   * iterate through all nodes and alert every node
   **/
  function alertNodes(){
    // We can now interact with the data
    for (var i = graph.length - 1; i >= 0; --i) {
      var p = graph[i];
      alertNode(p);
    }
  }

  /**
   * simple alert of a single node object
   **/
  function alertNode(node){
    alert("id: "+node.id+" | value: "+node.value+" | x: "+node.x+" | y: "+node.y);
  }

  /**
   * add a new object to the graph object
   **/
  function addNode(node,parent){
    graph.push(node);
  }

  /**
   * remove an object from the graph object
   */
  function removeNode(id){
    for (var i = graph.length - 1; i >= 0; --i) {
      if(graph[i].id === id){
        graph.splice(i,1); //delete
      }
    }
  }

  /**
   * edit a single node object
   */
  function edit(id,value,x,y){
    for (var i = graph.length - 1; i >= 0; --i) {
      if(graph[i].id === id){
        graph[i].value = value;
        graph[i].x = x;
        graph[i].y = y;
      }
    }
    alertNodes();
  }

});
